package com.hhu.softpractise.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hhu.softpractise.common.ErrorCode;
import com.hhu.softpractise.exception.BusinessException;
import com.hhu.softpractise.model.dto.post.PostQueryRequest;
import com.hhu.softpractise.model.entiy.Post;
import com.hhu.softpractise.model.entiy.PostFavour;
import com.hhu.softpractise.model.entiy.User;
import com.hhu.softpractise.service.PostFavourService;
import com.hhu.softpractise.mapper.PostFavourMapper;
import com.hhu.softpractise.service.PostService;
import org.springframework.aop.framework.AopContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
* @author 35534
* @description 针对表【post_favour(帖子收藏)】的数据库操作Service实现
* @createDate 2025-06-23 14:16:35
*/
@Service
public class PostFavourServiceImpl extends ServiceImpl<PostFavourMapper, PostFavour>
    implements PostFavourService{

    @Resource
    private PostService postService;

    @Override
    public int doPostFavour(long postId, User loginUser) {
        // 判断是否存在
        Post post = postService.getById(postId);
        if (post == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        // 是否已帖子收藏
        long userId = loginUser.getId();
        // 每个用户串行帖子收藏
        // 锁必须要包裹住事务方法
        PostFavourService postFavourService = (PostFavourService) AopContext.currentProxy();
        synchronized (String.valueOf(userId).intern()) {
            return postFavourService.doPostFavourInner(userId, postId);
        }
    }

    /**
     * 封装了事务的方法
     *
     * @param userId
     * @param postId
     * @return
     */
    @Override
    @Transactional
    public int doPostFavourInner(long userId, long postId) {
        PostFavour postFavour = new PostFavour();
        postFavour.setUserId(userId);
        postFavour.setPostId(postId);
        QueryWrapper<PostFavour> postFavourQueryWrapper = new QueryWrapper<>(postFavour);
        // 判断当前用户是否已收藏该帖子
        PostFavour oldPostFavour = this.getOne(postFavourQueryWrapper);
        if (oldPostFavour != null) {
            // 已收藏，取消收藏
            boolean remove = this.remove(postFavourQueryWrapper);
            if( remove){
                //找到帖子，收藏数-1
                Post post = postService.getById(postId);
                post.setFavourNum(post.getFavourNum() - 1);
                boolean update = postService.updateById(post);
                return update ? -1 : 0;
            }else{
                throw new BusinessException(ErrorCode.SYSTEM_ERROR);
            }
        } else {
            // 未收藏，添加收藏
            boolean save = this.save(postFavour);
            if( save){
                //找到帖子，收藏数+1
                Post post = postService.getById(postId);
                post.setFavourNum(post.getFavourNum() + 1);
                boolean update = postService.updateById(post);
                return update ? 1 : 0;
            }else{
                throw new BusinessException(ErrorCode.SYSTEM_ERROR);
            }
        }
    }

    /**
     * 获取用户收藏的帖子列表
     *
     * @param postQueryRequest
     * @param userId
     * @return
     */
    @Override
    public Page<Post> listFavourPostByPage(PostQueryRequest postQueryRequest, Long userId) {
        long current = postQueryRequest.getCurrent();
        long size = postQueryRequest.getPageSize();
        Page<Post> page = new Page<>(current, size);
        QueryWrapper<Post> queryWrapper = postService.getQueryWrapper(postQueryRequest);
        queryWrapper.eq("userId", userId);
        IPage< Post> result = postService.page(page, queryWrapper);
        List< Post> postList = result.getRecords();
        page.setRecords(postList);
        return page;
    }





}




